System.Array.AsReadOnly 方法
方法描述
返回指定数组的只读包装。
语法定义(C# System.Array.AsReadOnly 方法 的用法)
public static ReadOnlyCollectionAsReadOnly ( T[] array )
参数/返回值
参数值/返回值 | 参数类型/返回类型 | 参数描述/返回描述 |
---|---|---|
array | T[] | 要包装在只读 ReadOnlyCollection |
返回值 | System.Collections.ObjectModel.ReadOnlyCollection |
指定数组的只读 ReadOnlyCollection |
提示和注释
若要防止对数组进行任何修改,应该仅通过此包装公开数组。
只读集合只是一个具有用于防止修改的包装的集合;因此,如果更改基础集合,则只读集合将反映那些更改。
此方法的运算复杂度是 O(1)。
System.Array.AsReadOnly 方法例子
下面的示例将数组包装在只读 ReadOnlyCollection
using System; using System.Collections.Generic; public class SamplesArray { public static void Main() { // Create and initialize a new string array. String[] myArr = { "The", "quick", "brown", "fox" }; // Display the values of the array. Console.WriteLine( "The string array initially contains the following values:" ); PrintIndexAndValues( myArr ); // Create a read-only IList wrapper around the array. IListmyList = Array.AsReadOnly( myArr ); // Display the values of the read-only IList. Console.WriteLine( "The read-only IList contains the following values:" ); PrintIndexAndValues( myList ); // Attempt to change a value through the wrapper. try { myList[3] = "CAT"; } catch ( NotSupportedException e ) { Console.WriteLine( "{0} - {1}", e.GetType(), e.Message ); Console.WriteLine(); } // Change a value in the original array. myArr[2] = "RED"; // Display the values of the array. Console.WriteLine( "After changing the third element, the string array contains the following values:" ); PrintIndexAndValues( myArr ); // Display the values of the read-only IList. Console.WriteLine( "After changing the third element, the read-only IList contains the following values:" ); PrintIndexAndValues( myList ); } public static void PrintIndexAndValues( String[] myArr ) { for ( int i = 0; i < myArr.Length; i++ ) { Console.WriteLine( " [{0}] : {1}", i, myArr[i] ); } Console.WriteLine(); } public static void PrintIndexAndValues( IList myList ) { for ( int i = 0; i < myList.Count; i++ ) { Console.WriteLine( " [{0}] : {1}", i, myList[i] ); } Console.WriteLine(); } } /* This code produces the following output. The string array initially contains the following values: [0] : The [1] : quick [2] : brown [3] : fox The read-only IList contains the following values: [0] : The [1] : quick [2] : brown [3] : fox System.NotSupportedException - Collection is read-only. After changing the third element, the string array contains the following values: [0] : The [1] : quick [2] : RED [3] : fox After changing the third element, the read-only IList contains the following values: [0] : The [1] : quick [2] : RED [3] : fox */
异常
异常 | 异常描述 |
---|---|
ArgumentNullException | array 为 null。 |
版本信息
.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1
适用平台
Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。